package net.cyue.nest.nestjava.platform.tomcat;

import com.fasterxml.jackson.core.JsonProcessingException;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletResponse;
import net.cyue.nest.nestjava.common.data.http.HTTPRequestBody;
import net.cyue.nest.nestjava.common.enums.HTTPStatus;
import net.cyue.nest.nestjava.common.util.JSONUtil;
import net.cyue.nest.nestjava.core.adapter.AbstractHTTPResponse;

import java.io.IOException;
import java.io.PrintWriter;

public class TomcatHTTPResponse
  extends AbstractHTTPResponse<HttpServletResponse>
{
  private final PrintWriter out;

  public TomcatHTTPResponse(ServletResponse response) {
    this.response = (HttpServletResponse) response;
    this.response.setCharacterEncoding("utf-8");
    try {
      this.out = this.response.getWriter();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  @Override
  public void status(HTTPStatus status) {
    this.response.setStatus(status.code());
  }

  @Override
  public void putHeader(String name, String value) {
    if (this.response.getHeader(name) == null) {
      this.response.addHeader(name, value);
    } else {
      this.response.setHeader(name, value);
    }
  }

  @Override
  public void removeHeader(String name) {
    if (this.response.containsHeader(name)) {
      this.response.setHeader(name, "");
    }
  }

  @Override
  public void redirect(HTTPStatus status, String url) {
    this.status(status);
    try {
      this.response.sendRedirect(url);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  @Override
  public void redirect(String url) {
    try {
      this.response.sendRedirect(url);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  @Override
  public void end(String s) {
    if (this.out == null) {
      System.err.println("writer is null");
      return;
    }

    this.out.print(s);
    this.out.flush();
    this.out.close();
  }

  @Override
  public void send(String body) {
    if (this.out == null) {
      System.err.println("writer is null");
      return;
    }

    try {
      final var b = HTTPRequestBody.fromJSONString(body);
      this.out.print(b);
    } catch (JsonProcessingException e) {
      this.out.print(body);
    }
  }
  @Override
  public void send(Object body) {
    if (this.out == null) {
      System.err.println("writer is null");
      return;
    }

    String jsonString = JSONUtil.toJSONString(body);
    this.response.setContentType("application/json;charset=utf-8");
    this.out.print(jsonString);
  }

  @Override
  public void write(String s) {
    if (this.out == null) {
      System.err.println("writer is null");
      return;
    }
    this.out.print(s);
  }
}
